package com.jyy.prefabricated.controller;


import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.entity.CertificateType;
import com.jyy.prefabricated.service.CertificateTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 证书类型
 * @version 1.0.0
 * @author: zjj
 */
@Slf4j
@RequestMapping("/api")
@RestController
public class CertificateTypeController {

    @Autowired
    private CertificateTypeService typeService;

    /**
     * 新增证书类型
     * @param certificateType
     * @return
     */
    @PostMapping("/certificate-type")
    public AjaxResult insert(@Valid @RequestBody CertificateType certificateType){
        log.info("Rest request to insert certificateType:{}",certificateType);
        Integer id = typeService.insert(certificateType);
        CertificateType result = typeService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 更新证书类型
     * @param certificateType
     * @return
     */
    @PutMapping("/certificate-type")
    public AjaxResult update(@Valid @RequestBody CertificateType certificateType){
        log.info("Rest request to update certificateType:{}",certificateType);
        typeService.update(certificateType);
        CertificateType result = typeService.selectById(certificateType.getId());
        return AjaxResult.success(result);
    }

    /**
     * 查询证书类型详情
     * @param id
     * @return
     */
    @GetMapping("/certificate-type/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        log.info("Rest request to find certificateType by id:{}",id);
        CertificateType result = typeService.selectById(id);
        return AjaxResult.success(result);
    }

    /**
     * 删除证书类型
     * @param id
     * @return
     */
    @DeleteMapping("/certificate-type/{id}")
    public AjaxResult deleteById(@PathVariable Integer id){
        log.info("Rest request to delete certificateType by id:{}",id);
        int row = typeService.delete(id);
        if(row > 0){
            return AjaxResult.success();
        } else {
            return AjaxResult.error();
        }
    }

    /**
     * 查询证书类型列表
     * @return
     */
    @GetMapping("/certificate-type")
    public AjaxResult selectById(){
        log.info("Rest request to find certificateType list");
        List<CertificateType> result = typeService.selectAll();
        return AjaxResult.success(result);
    }
}
